package com.jeesite.modules.stock.service;

import com.jeesite.modules.stock.dao.UserRequestLogsDao;
import com.jeesite.modules.stock.entity.UserRequestLogsEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Service
public class UserRequestLogsService {

    @Autowired
    private UserRequestLogsDao userRequestLogsDao;

    public void addRequestLog(String userId, String interfaceName) {
        UserRequestLogsEntity entity = new UserRequestLogsEntity();
        entity.setUserId(userId);
        entity.setInterfaceName(interfaceName);
        entity.setReqTime(new Date());
        userRequestLogsDao.insert(entity);
    }

    public int getRequestCountAfterTime(String userId, String interfaceName, TimeUnit timeUnit) {
        Date time = new Date();
        if (timeUnit == TimeUnit.MINUTES) {
            time.setTime(System.currentTimeMillis() - 1000 * 60);
        } else if (timeUnit == TimeUnit.HOURS) {
            time.setTime(System.currentTimeMillis() - 1000 * 60 * 60);
        }
        return userRequestLogsDao.getRequestCountAfterTime(userId, interfaceName, time);
    }
}
